/**
 * 初始化工具
 */

import CesiumNavigation from 'cesium-navigation-es6'
import type { NavigationOptions } from 'cesium-navigation-es6'

class Controller {
  // 初始化 controller 类
  viewer: any;
  constructor(BaseMapConfig: any) {
    this.init_data();
  }
  init_data() {
    this.viewer = null;
  }
  init(BaseMapConfig: any) {
    const mapID = "cesium-container";
    let vConfig = {
      navigation: false, // 默认为true，是否显示导航罗盘控件。隐藏可在初始化场景时设置为false
      animation: false, //是否创建动画小器件，左下角仪表
      baseLayerPicker: false, //是否显示图层选择器,设置为true会报错：底图加载失败
      fullscreenButton: false, //是否显示全屏按钮
      geocoder: false, //是否显示geocoder搜索定位小器件，右上角查询按钮
      homeButton: false, //是否显示Home按钮 回归到地球初始视角
      infoBox: false, //是否显示信息框 一般点击模型图层的时候右上角出现的信息框的显隐
      sceneModePicker: true, //是否显示3D/2D模式选择器
      selectionIndicator: true, //是否显示选取指示器组件,例如点线面的点击聚焦等
      mapProjection: new Cesium.WebMercatorProjection(), //地图投影体系
      terrainProvider: Cesium.createWorldTerrain() // 调用地形服务
    };
    vConfig = Object.assign(vConfig, BaseMapConfig);

    const viewer = new Cesium.Viewer(mapID, vConfig);

    this.viewer = viewer;
    // 抗锯齿
    viewer.scene.fxaa = true;
    viewer.scene.postProcessStages.fxaa.enabled = false;
    // 水雾特效
    viewer.scene.globe.showGroundAtmosphere = true;
    // 设置最大俯仰角，[-90,0]区间内，默认为-30，单位弧度
    viewer.scene.screenSpaceCameraController.constrainedPitch =
      Cesium.Math.toRadians(0);
    viewer.scene.screenSpaceCameraController.autoResetHeadingPitch = false;
    viewer.scene.screenSpaceCameraController.inertiaZoom = 0.5;
    viewer.scene.screenSpaceCameraController.minimumZoomDistance = 50;
    viewer.scene.screenSpaceCameraController.maximumZoomDistance = 20000000;
    viewer.scene.screenSpaceCameraController.zoomEventTypes = [
      Cesium.CameraEventType.RIGHT_DRAG,
      Cesium.CameraEventType.WHEEL,
      Cesium.CameraEventType.PINCH,
    ];
    viewer.scene.screenSpaceCameraController.tiltEventTypes = [
      Cesium.CameraEventType.MIDDLE_DRAG,
      Cesium.CameraEventType.PINCH,
      {
        eventType: Cesium.CameraEventType.LEFT_DRAG,
        modifier: Cesium.KeyboardEventModifier.CTRL,
      },
      {
        eventType: Cesium.CameraEventType.RIGHT_DRAG,
        modifier: Cesium.KeyboardEventModifier.CTRL,
      },
    ];
    // 取消默认的双击事件
    viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(
      Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK
    );

    this.initCesiumNavigation(viewer)
    return viewer;
  }
  initCesiumNavigation(viewer: any) {
    const options: NavigationOptions  = {}
    // 用于在使用重置导航重置地图视图时设置默认视图控制。接受的值是Cesium.Cartographic 和 Cesium.Rectangle.
    options.defaultResetView = new Cesium.Rectangle.fromDegrees(80, 22, 130, 50);
    //相机延时
    // options.duration = 4//默认为3s
    
    // 用于启用或禁用罗盘。true是启用罗盘，false是禁用罗盘。默认值为true。如果将选项设置为false，则罗盘将不会添加到地图中。
    options.enableCompass = true;
    // 用于启用或禁用缩放控件。true是启用，false是禁用。默认值为true。如果将选项设置为false，则缩放控件将不会添加到地图中。
    options.enableZoomControls = true;
    // 用于启用或禁用距离图例。true是启用，false是禁用。默认值为true。如果将选项设置为false，距离图例将不会添加到地图中。
    options.enableDistanceLegend = true;
    // 用于启用或禁用指南针外环。true是启用，false是禁用。默认值为true。如果将选项设置为false，则该环将可见但无效。
    options.enableCompassOuterRing = true;
    
    //修改重置视图的tooltip
    options.resetTooltip = "重置视图";
    //修改放大按钮的tooltip
    options.zoomInTooltip = "放大";
    //修改缩小按钮的tooltip
    options.zoomOutTooltip = "缩小";
    
    new CesiumNavigation(viewer, options)
  }
  
  // 将三维球定位到中国
  flyTo(config:any) {
    let Vconfig = config || {
      x: 103.84,
      y: 31.15,
      z: 17850000,
      heading: 348.4202942851978,
      pitch: -89.74026687972041,
      roll: 0
    }
    this.viewer.camera.flyTo({
      destination: Cesium.Cartesian3.fromDegrees(Vconfig.x, Vconfig.y, Vconfig.z),
      orientation: {
        heading: Cesium.Math.toRadians(Vconfig.heading),
        pitch: Cesium.Math.toRadians(Vconfig.pitch),
        roll: Cesium.Math.toRadians(Vconfig.roll),
      },
      complete: function callback() {
        // 定位完成之后的回调函数
      },
    });
  };

  // 获取当前视图的中心经纬度
  getCurCenterlonLat(viewer: any) {
    let result = viewer.camera.pickEllipsoid(
      new Cesium.Cartesian2(
        viewer.canvas.clientWidth / 2,
        viewer.canvas.clientHeight / 2
      )
    )
    let curPosition = Cesium.Ellipsoid.WGS84.cartesianToCartographic(result)
    let lon = (curPosition.longitude * 180) / Math.PI
    let lat = (curPosition.latitude * 180) / Math.PI
    return {
      lon: lon,
      lat: lat,
    }
  }

  /**
   * 手动设置场景的二三维展示
   * @param type 
   * SCENE3D 三维场景
   * COLUMBUS_VIEW 平面场景
   */
  changeSceneModeHandler(type: string) {
    this.viewer.scene.mode = Cesium.SceneMode[type];
  }

  /**
   * 移除图层
   * @param {number} index 要检索的索引
   */
  removeImageryLayers(index:number) {
    this.viewer.imageryLayers.remove(this.viewer.imageryLayers.get(index))
  }

  /**
   * 
   * @param {number} index 要检索的索引
   * @param {number} alpha 透明度 0-1
   */
  ImageryLayersAlpha(index:number,alpha:number) {
    this.viewer.imageryLayers.get(index).alpha = alpha
  }
}

export const GController = new Controller();
